package it.webrules.deploymanager.webapp;

import it.webrules.deploymanager.webapp.controller.OperationController;
import it.webrules.deploymanager.webapp.transentities.Operation;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.UriBuilder;

public class OperationDescription {

	private final UriBuilder uriBuilder;
	private String pathfilename;

	public OperationDescription(UriBuilder uriBuilder, String path, String filename) {
		this.uriBuilder = uriBuilder;
		this.pathfilename = path + "/" + filename;
	}

	public List<Operation> getStaticOperation() {
		List<Operation> ops = getCommonOperation();

		Operation operation = new Operation("TagTrunkAndDeploy");
		ops.add(operation);

		operation = new Operation("Checkout");
		ops.add(operation);

		operation = new Operation("Update");
		ops.add(operation);

		operation = new Operation("Info");
		ops.add(operation);

		operation = new Operation("Cleanup");
		ops.add(operation);

		operation = new Operation("StatusFile");
		ops.add(operation);

		operation = new Operation("DiffContent");
		ops.add(operation);

		operation = new Operation("Revert");
		ops.add(operation);

		operation = new Operation("CreateTagOrBranch");
		ops.add(operation);

		operation = new Operation("DeleteBranch");
		ops.add(operation);

		return ops;
	}

	public List<Operation> getDinamycOperation() {
		List<Operation> ops = getCommonOperation();

		Operation operation = new Operation("BuildAndDeploy");
		ops.add(operation);

		return ops;
	}

	private List<Operation> getCommonOperation() {
		List<Operation> ops = new ArrayList<>();

		Operation operation = new Operation("ListDeployableRelease");
		operation.setPath(uriBuilder.clone().path(OperationController.class).path(getM("getListDeployableRelease")).build(pathfilename));
		ops.add(operation);

		operation = new Operation("Deploy");
		ops.add(operation);

		operation = new Operation("NextDefaultVersions");
		ops.add(operation);

		operation = new Operation("Log");
		ops.add(operation);

		operation = new Operation("CreateSimpleGraph");
		ops.add(operation);

		operation = new Operation("CreateCompleteGraph");
		ops.add(operation);

		return ops;
	}

	private Method getM(String nameMethod) {

		Method[] methods = OperationController.class.getMethods();
		for (Method method : methods) {
			if (method.getName().equals(nameMethod))
				return method;
		}

		return null;

	}

}
